import type { RouteList, IRouteModule } from '@/types/router/index.ts'
import NotFount from '@/components/Layout/NotFount/NotFount.vue'
// util
import { isObject } from '@/utils/object'

// 获取所有路由模块
const routeFiles: IRouteModule = import.meta.glob('./modules/*.ts', { eager: true })
// console.log(routeFiles)

export const setRoutes = (routes: RouteList) => {
  Object.keys(routeFiles).forEach((route) => {
    const currentRoute = routeFiles[route].default
    if (Array.isArray(currentRoute)) {
      routes.push(...currentRoute)
    } else if (isObject(currentRoute)) {
      routes.push(currentRoute)
    } else {
      console.warn('[warn setRoute]: type error addition failed : ' + currentRoute)
    }
  })
  // 404 页面放在最后加入
  routes.push({
    path: '/:notFound(.*)*',
    name: 'notFound',
    component: NotFount,
  })
}
